<template>
  <div>
    <!-- 评论列表 -->
    <div class="cmt-list" :class="!isShowCmtInput ? 'art-cmt-container-2' : 'art-cmt-container-1'">
      <!-- 评论的 Item 项 -->
      <div class="cmt-item">
        <!-- 头部区域 -->
        <div class="cmt-header">
          <!-- 头部左侧 -->
          <div class="cmt-header-left">
            <img :src="obj.aut_photo" alt="" class="avatar" />
            <span class="uname">{{ obj.aut_name }}</span>
          </div>
          <!-- 头部右侧 -->
          <div class="cmt-header-right">
            <van-icon
              v-if="obj.is_liking === true"
              name="like"
              size="16"
              color="red"
              @click="loveFn(true, obj)"
            />
            <van-icon
              v-else
              name="like-o"
              size="16"
              color="gray"
              @click="loveFn(false, obj)"
            />
          </div>
        </div>
        <!-- 主体区域 -->
        <div class="cmt-body">
          {{ obj.content }}
        </div>
        <!-- 尾部区域 -->
        <div class="cmt-footer">{{ timeAgo(obj.pubdate) }}</div>
      </div>

    </div>
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date'
import { commentLikingAPI, commentDisLikingAPI } from '@/api'
export default {
  props: {
    obj: {
      type: Object
    },
    isShowCmtInput: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    timeAgo,
    async loveFn (val, item) {
      if (val === true) {
        item.is_liking = false
        await commentDisLikingAPI({
          comId: item.com_id
        })
      } else {
        item.is_liking = true
        await commentLikingAPI({
          comId: item.com_id
        })
      }
    }
  },
  async created () {}
}
</script>

<style scoped lang="less">
.cmt-list {
  padding: 10px;
  text-align: left;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
    }
  }
}

/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}
</style>
